//index.js
//获取应用实例
const app = getApp();
let globalData = getApp().globalData;
let utils = require('../../utils/utils')

// 引用百度地图微信小程序JSAPI模块 
var bmap = require('../../libs/bmap-wx.js'); 

Page({
  data: {
    bcgImg: '/img/beach-bird-birds-235787.jpg',
    bcgImgAreaShow:false,
    bcgImgIndex:0,
    bcgImgList: [
      {
        src: '/img/beach-bird-birds-235787.jpg',
        topColor: '#393836'
      },
      {
        src: '/img/clouds-forest-idyllic-417102.jpg',
        topColor: '#0085e5'
      },
      {
        src: '/img/backlit-dawn-dusk-327466.jpg',
        topColor: '#2d2225'
      },
      {
        src: '/img/accomplishment-adventure-clear-sky-585825.jpg',
        topColor: '#004a89'
      },
      {
        src: '/img/fog-himalayas-landscape-38326.jpg',
        topColor: '#b8bab9'
      },
      {
        src: '/img/asphalt-blue-sky-clouds-490411.jpg',
        topColor: '#009ffe'
      },
      {
        src: '/img/aerial-climate-cold-296559.jpg',
        topColor: '#d6d1e6'
      },
      {
        src: '/img/beautiful-cold-dawn-547115.jpg',
        topColor: '#ffa5bc'
      }
    ],
    bcgColor: '#444',
    searchText:'',

    cityDatas: {},
    icons: ['/img/clothing.png', '/img/carwashing.png', '/img/pill.png', '/img/running.png', '/img/sun.png'],
  },
  onLoad(){
    this.init();
  },
  onShow(){
    this.getCityDatas();
    this.setBcgImg();
  },
  onPullDownRefresh () {
    this.init();
  },
  onShareAppMessage (res) {
    return {
      title: '一霎秋风-大白の秋风',
      path: `/pages/index/index`,
      success() {},
      fail(e) {
        let errMsg = e.errMsg || ''
        // 对不是用户取消转发导致的失败进行提示
        let msg = '分享失败，可重新分享'
        if (errMsg.indexOf('cancel') !== -1) {
          msg = '取消分享'
        }
        wx.showToast({
          title: msg,
          icon: 'none',
        })
      }
    }
  },
  chooseBcg(e){
    let dataset = e.currentTarget.dataset;
    let src = dataset.src;
    let index = dataset.index;
    this.setBcgImg(index);
    wx.setStorage({
      key : 'bcgImgIndex',
      data : index
    })
  },
  setBcgImg(index){
    if(index != undefined){
      this.setData({
        bcgImgIndex : index,
        bcgImg : this.data.bcgImgList[index].src,
        bcgColor : this.data.bcgImgList[index].topColor
      })
      this.setNavigationBarColor();
    }else{
      wx.getStorage({
        key : 'bcgImgIndex',
        success:(res) => {
          let bcgImgIndex = res.data || 0;
          this.setData({
            bcgImgIndex : bcgImgIndex,
            bcgImg: this.data.bcgImgList[bcgImgIndex].src,
            bcgColor: this.data.bcgImgList[bcgImgIndex].topColor,
          });
          this.setNavigationBarColor();
        },
        fail:()=>{
          this.setData({
            bcgImgIndex : 0,
            bcgImg: this.data.bcgImgList[0].src,
            bcgColor: this.data.bcgImgList[0].topColor,
          });
          this.setNavigationBarColor();
        }
      })
    }
  },
  setNavigationBarColor(){
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: this.data.bcgColor,
    })
  },
  showBcgImgArea(){
    this.setData({
      bcgImgAreaShow : true
    })
  },
  hideBcgImgArea(){
    this.setData({
      bcgImgAreaShow : false
    })
  },
  getCityDatas(){
    let cityDatas = wx.getStorage({
      key: 'cityDatas',
      success:(res)=>{
        this.setData({
          cityDatas : res.data
        })
      }
    })
  },
  init(params){
    let that = this;
    // 新建百度地图对象 
    let BMap = new bmap.BMapWX({
      ak : globalData.ak
    })
    // 发起weather请求
    BMap.weather({
      // location: params.location,
      fail:this.fail,
      success:this.success
    })
  },
  success(data){
    let now = new Date();
    let results = data.originalData.results[0] || {};

    //存储数据
    data.updateTime = now.getTime();
    data.updateTimeFormat = utils.formatTime(now)
    data.temperature = `${results.weather_data[0].date.match(/\d+/g)[2]}`
    data.pm = this.calcPM(results['pm25'])

    wx.setStorage({
      key: 'cityDatas',
      data: data,
    })
    this.setData({
      cityDatas : data
    })
    console.log(data);
  },
  fail(res){
    console.log(res);
  },
  calcPM(value){
    if (value > 0 && value <= 50) {
      return {
        val: value,
        desc: '优',
        detail: '',
      }
    } else if (value > 50 && value <= 100) {
      return {
        val: value,
        desc: '良',
        detail: '',
      }
    } else if (value > 100 && value <= 150) {
      return {
        val: value,
        desc: '轻度污染',
        detail: '对敏感人群不健康',
      }
    } else if (value > 150 && value <= 200) {
      return {
        val: value,
        desc: '中度污染',
        detail: '不健康',
      }
    } else if (value > 200 && value <= 300) {
      return {
        val: value,
        desc: '重度污染',
        detail: '非常不健康',
      }
    } else if (value > 300 && value <= 500) {
      return {
        val: value,
        desc: '严重污染',
        detail: '有毒物',
      }
    } else if (value > 500) {
      return {
        val: value,
        desc: '爆表',
        detail: '能出来的都是条汉子',
      }
    }
  }
})
